/*
 * Copyright (C) 2016 The Fosafer Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fosafer.idcard_demo.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.fosafer.idcard_demo.R;
import com.fosafer.idcard_demo.util.ConstantsUtil;
import com.fosafer.idcard_demo.util.ImageUtils;


/**
 * Package:      [com.fosafer.idcard_demo.view]
 * ClassName:    [CustomProgressDialog]
 * Description:  [自定义等待框]
 * Author:       [cuijianzhi]
 * CreateDate:   [2016/04/12 15:22]
 * UpdateUser:   [cuijianzhi]
 * UpdateDate:   [2016/04/12 15:22]
 * UpdateRemark: [Update Description]
 * Version:      [v1.0.0]
 * <p>
 * Copyright (c) 2016 Fosafer Co.,Ltd. All rights reserved.
 */
public class CustomProgressDialog extends ProgressDialog {
    public static final int SHOW_TYPE_IMAGE = 0;
    public static final int SHOW_TYPE_TV = 1;
    private ImageView mImageView;
    private int showType = SHOW_TYPE_IMAGE;
    //图片数组对象
    private final int[] mFrameBGs = {
            R.drawable.icon_gif1,
            R.drawable.icon_gif2,
            R.drawable.icon_gif3,
            R.drawable.icon_gif4,
            R.drawable.icon_gif5
    };
    //播放至最后一张图片的下标
    private int mLastFrameNo;
    private boolean isStopPlay;


    public CustomProgressDialog(Context context, int theme, int type) {
        super(context, theme);
        setCanceledOnTouchOutside(false);
        this.showType = type;
        isStopPlay = false;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
        setCanceledOnTouchOutside(false);
        this.showType = SHOW_TYPE_IMAGE;
        isStopPlay = false;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initData() {
        if (showType == SHOW_TYPE_IMAGE) {
            mLastFrameNo = mFrameBGs.length - 1;
            mImageView.setImageBitmap(ImageUtils.readBitMap(mFrameBGs[0]));
            play(1);
        }

    }

    private void play(final int pFrameNo) {
        mImageView.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (!isStopPlay) {
                    mImageView.setImageBitmap(ImageUtils.readBitMap(mFrameBGs[pFrameNo]));
                    // 当播放完成后
                    if (pFrameNo == mLastFrameNo) {
                        play(0);
                    } else {
                        play(pFrameNo + 1);
                    }
                }
            }

        }, ConstantsUtil.TIME_200);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        isStopPlay = true;
    }

    private void initView() {
        setContentView(R.layout.layout_progress_dialog);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setAttributes(params);
        mImageView = (ImageView) findViewById(R.id.layout_progress_dialog_gv);
        TextView layout_progress_dialog_tv_msg = (TextView) findViewById(R.id.layout_progress_dialog_tv_msg);
        if (showType == SHOW_TYPE_IMAGE) {
            mImageView.setVisibility(View.VISIBLE);
            layout_progress_dialog_tv_msg.setVisibility(View.GONE);
        } else if (showType == SHOW_TYPE_TV) {
            layout_progress_dialog_tv_msg.setVisibility(View.VISIBLE);
            mImageView.setVisibility(View.GONE);
        }
    }


}